package com.itbaizhan.clouddemooauth2server.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

public class Md5Utils {

    //生成随机盐
    public static byte[] generateSale(){
        SecureRandom secureRandom = new SecureRandom();
        byte[] salt = new byte[16];
        secureRandom.nextBytes(salt);
        return salt;
    }
    public static String encrypt(String password, byte[] salt) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(salt); // 加入盐
            md.update(password.getBytes()); // 加入密码
            byte[] hashedPassword = md.digest();
            return Base64.getEncoder().encodeToString(hashedPassword); // 以 Base64 格式返回哈希值
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt2(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(password.getBytes()); // 加入密码
            byte[] hashedPassword = md.digest();
            return Base64.getEncoder().encodeToString(hashedPassword); // 以 Base64 格式返回哈希值
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }





}
